
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>面向对象编程 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="类的特殊方法.html" />
    
    
    <link rel="prev" href="函数式编程.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    前言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="基础.html">
            
                <a href="基础.html">
            
                    
                    基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="字符串处理.html">
            
                <a href="字符串处理.html">
            
                    
                    字符串处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="数据结构.html">
            
                <a href="数据结构.html">
            
                    
                    数据结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="数据结构高级内容.html">
            
                <a href="数据结构高级内容.html">
            
                    
                    数据结构高级内容
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="函数.html">
            
                <a href="函数.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="函数式编程.html">
            
                <a href="函数式编程.html">
            
                    
                    函数式编程
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.8" data-path="面向对象编程.html">
            
                <a href="面向对象编程.html">
            
                    
                    面向对象编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="类的特殊方法.html">
            
                <a href="类的特殊方法.html">
            
                    
                    类的特殊方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="异常处理.html">
            
                <a href="异常处理.html">
            
                    
                    异常处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="IO编程.html">
            
                <a href="IO编程.html">
            
                    
                    IO编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="模块.html">
            
                <a href="模块.html">
            
                    
                    模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="标准库.html">
            
                <a href="标准库.html">
            
                    
                    标准库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="测试.html">
            
                <a href="测试.html">
            
                    
                    测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="调试.html">
            
                <a href="调试.html">
            
                    
                    调试
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >面向对象编程</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x9762;&#x5411;&#x5BF9;&#x8C61;&#x7F16;&#x7A0B;">&#x9762;&#x5411;&#x5BF9;&#x8C61;&#x7F16;&#x7A0B;</h1>
<hr>
<h2 id="&#x52A8;&#x6001;&#x8BED;&#x8A00;&#x7684;&#x9E2D;&#x5B50;&#x7C7B;&#x578B;">&#x52A8;&#x6001;&#x8BED;&#x8A00;&#x7684;&#x201C;&#x9E2D;&#x5B50;&#x7C7B;&#x578B;&#x201D;</h2>
<p>&#x5BF9;&#x4E8E;&#x9759;&#x6001;&#x8BED;&#x8A00;&#xFF08;&#x4F8B;&#x5982; Java&#xFF09;&#x6765;&#x8BF4;&#xFF0C;&#x5982;&#x679C;&#x9700;&#x8981;&#x4F20;&#x5165; Animal &#x7C7B;&#x578B;&#xFF0C;&#x5219;&#x4F20;&#x5165;&#x7684;&#x5BF9;&#x8C61;&#x5FC5;&#x987B;&#x662F; Animal &#x7C7B;&#x578B;&#x6216;&#x8005;&#x5B83;&#x7684;&#x5B50;&#x7C7B;&#xFF0C;&#x5426;&#x5219;&#xFF0C;&#x5C06;&#x65E0;&#x6CD5;&#x8C03;&#x7528; run()&#x65B9;&#x6CD5;&#x3002;  </p>
<p>&#x5BF9;&#x4E8E; Python &#x8FD9;&#x6837;&#x7684;&#x52A8;&#x6001;&#x8BED;&#x8A00;&#x6765;&#x8BF4;&#xFF0C;&#x5219;&#x4E0D;&#x4E00;&#x5B9A;&#x9700;&#x8981;&#x4F20;&#x5165; Animal &#x7C7B;&#x578B;&#x3002;&#x6211;&#x4EEC;&#x53EA;&#x9700;&#x8981;&#x4FDD;&#x8BC1;&#x4F20;&#x5165;&#x7684;&#x5BF9;&#x8C61;&#x6709;&#x4E00;&#x4E2A; run()&#x65B9;&#x6CD5;&#x5C31;&#x53EF;&#x4EE5;&#x4E86;&#xFF1A;  &#x8FD9;&#x5C31;&#x662F;&#x52A8;&#x6001;&#x8BED;&#x8A00;&#x7684;&#x201C;&#x9E2D;&#x5B50;&#x7C7B;&#x578B;&#x201D;&#xFF0C;&#x5B83;&#x5E76;&#x4E0D;&#x8981;&#x6C42;&#x4E25;&#x683C;&#x7684;&#x7EE7;&#x627F;&#x4F53;&#x7CFB;&#xFF0C;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x53EA;&#x8981;&#x201C;&#x770B;&#x8D77;&#x6765;&#x50CF;&#x9E2D;&#x5B50;&#xFF0C;&#x8D70;&#x8D77;&#x8DEF;&#x6765;&#x50CF;&#x9E2D;&#x5B50;&#x201D;&#xFF0C;&#x90A3;&#x5B83;&#x5C31;&#x53EF;&#x4EE5;&#x88AB;&#x770B;&#x505A;&#x662F;&#x9E2D;&#x5B50;&#x3002;  </p>
<h2 id="&#x83B7;&#x53D6;&#x5BF9;&#x8C61;&#x4FE1;&#x606F;">&#x83B7;&#x53D6;&#x5BF9;&#x8C61;&#x4FE1;&#x606F;</h2>
<p>1&#xFF09;&#x83B7;&#x53D6;&#x57FA;&#x672C;&#x7C7B;&#x578B;&#xFF0C;&#x4F7F;&#x7528;<code>type()</code>&#x51FD;&#x6570;&#xFF1A;  </p>
<pre><code class="lang-python">&gt;&gt;&gt; type(123)
&lt;class &apos;int&apos;&gt;
&gt;&gt;&gt; type(123)==type(456)
True
&gt;&gt;&gt; type(123)==int
True
</code></pre>
<p>2&#xFF09;&#x5BF9;&#x4E8E; class &#x7684;&#x7EE7;&#x627F;&#x5173;&#x7CFB;&#x6765;&#x8BF4;&#xFF0C;&#x4F7F;&#x7528; <code>type()</code>&#x5C31;&#x5F88;&#x4E0D;&#x65B9;&#x4FBF;&#x3002;&#x5224;&#x65AD; class&#x7684;&#x7C7B;&#x578B;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>isinstance()</code>&#x51FD;&#x6570;&#xFF1A;<br><code>isinstance()</code>&#x5224;&#x65AD;&#x7684;&#x662F;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x662F;&#x5426;&#x662F;&#x8BE5;&#x7C7B;&#x578B;&#x672C;&#x8EAB;&#xFF0C;&#x6216;&#x8005;&#x4F4D;&#x4E8E;&#x8BE5;&#x7C7B;&#x578B;&#x7684;&#x7236;&#x7EE7;&#x627F;&#x94FE;&#x4E0A;&#x3002;<br>&#x5982;&#x679C;&#x7EE7;&#x627F;&#x5173;&#x7CFB;&#x662F;&#xFF1A;<br>object -&gt; Animal -&gt; Dog -&gt; Husky</p>
<p>&#x90A3;&#x4E48;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>a = Animal()
<span class="hljs-meta">&gt;&gt;&gt; </span>d = Dog()
<span class="hljs-meta">&gt;&gt;&gt; </span>h = Husky()
<span class="hljs-meta">&gt;&gt;&gt; </span>isinstance(h, Husky)
<span class="hljs-keyword">True</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>isinstance(h, Dog)
<span class="hljs-keyword">True</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>isinstance(h, Animal)
<span class="hljs-keyword">True</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>isinstance(d, Husky)
<span class="hljs-keyword">False</span>
</code></pre>
<p>&#x80FD;&#x7528; <code>type()</code>&#x5224;&#x65AD;&#x7684;&#x57FA;&#x672C;&#x7C7B;&#x578B;&#x4E5F;&#x53EF;&#x4EE5;&#x7528; <code>isinstance()</code>&#x5224;&#x65AD;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>isinstance(<span class="hljs-string">&apos;a&apos;</span>, str)
<span class="hljs-keyword">True</span>
</code></pre>
<p>&#x5E76;&#x4E14;&#x8FD8;&#x53EF;&#x4EE5;&#x5224;&#x65AD;&#x4E00;&#x4E2A;&#x53D8;&#x91CF;&#x662F;&#x5426;&#x662F;&#x67D0;&#x4E9B;&#x7C7B;&#x578B;&#x4E2D;&#x7684;&#x4E00;&#x79CD;&#xFF0C;&#x6BD4;&#x5982;&#x4E0B;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x5C31;&#x53EF;&#x4EE5;&#x5224;&#x65AD;&#x662F;&#x5426;&#x662F; list &#x6216;&#x8005; tuple&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>isinstance([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>], (list, tuple))
<span class="hljs-keyword">True</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>isinstance((<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>), (list, tuple))
<span class="hljs-keyword">True</span>
</code></pre>
<p>3&#xFF09;&#x83B7;&#x5F97;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x7684;&#x6240;&#x6709;&#x5C5E;&#x6027;&#x548C;&#x65B9;&#x6CD5;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>dir()</code>&#x51FD;&#x6570;&#xFF0C;&#x5B83;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x5305;&#x542B;&#x5B57;&#x7B26;&#x4E32;&#x7684; list&#xFF0C;&#x6BD4;&#x5982;&#xFF0C;&#x83B7;&#x5F97;&#x4E00;&#x4E2A; str &#x5BF9;&#x8C61;&#x7684;&#x6240;&#x6709;&#x5C5E;&#x6027;&#x548C;&#x65B9;&#x6CD5;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>dir(<span class="hljs-string">&apos;ABC&apos;</span>)
</code></pre>
<p>4&#xFF09;&#x4F7F;&#x7528; <code>hasattr()</code>&#x3001;<code>getattr()</code>&#x548C;<code>setattr()</code>&#xFF0C;&#x6765;&#x6D4B;&#x8BD5;&#x5C5E;&#x6027;&#x662F;&#x5426;&#x5B58;&#x5728;&#x3001;&#x83B7;&#x53D6;&#x4E00;&#x4E2A;&#x5C5E;&#x6027;&#x548C;&#x8BBE;&#x7F6E;&#x4E00;&#x4E2A;&#x5C5E;&#x6027;&#x3002;<br>&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x4E86;&#x5982;&#x4E0B;&#x7684;&#x7C7B;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">My</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self)</span>:</span>
        self.x = <span class="hljs-number">9</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">show</span><span class="hljs-params">(self)</span>:</span>
        print(x)
<span class="hljs-meta">&gt;&gt;&gt; </span>my = My()
<span class="hljs-meta">&gt;&gt;&gt; </span>hasattr(my, <span class="hljs-string">&apos;x&apos;</span>) <span class="hljs-comment"># &#x6709;&#x5C5E;&#x6027;&apos;x&apos;&#x5417;&#xFF1F;</span>
<span class="hljs-keyword">True</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>hasattr(my, <span class="hljs-string">&apos;show&apos;</span>) <span class="hljs-comment"># &#x6709;&#x5C5E;&#x6027;&apos;show&apos;&#x5417;&#xFF1F;</span>
<span class="hljs-keyword">True</span>
</code></pre>
<h2 id="&#x7C7B;&#x7684;&#x5B9A;&#x4E49;">&#x7C7B;&#x7684;&#x5B9A;&#x4E49;</h2>
<p>&#x7C7B;&#x53D8;&#x91CF;&#x3001;&#x7C7B;&#x65B9;&#x6CD5;&#xFF08;&#x9759;&#x6001;&#x65B9;&#x6CD5;&#xFF09;&#xFF1A; <code>&#x7C7B;&#x540D;.xxx</code>       or     <code>&#x5B9E;&#x4F8B;&#x540D;.xxx</code>
&#x5B9E;&#x4F8B;&#x53D8;&#x91CF;&#x3001;&#x5B9E;&#x4F8B;&#x65B9;&#x6CD5;:          <code>&#x5B9E;&#x4F8B;&#x540D;.xxx</code></p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">School</span>:</span>
    population = <span class="hljs-number">0</span>   <span class="hljs-comment">#&#x7C7B;&#x53D8;&#x91CF;</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, name)</span>:</span>
        School.population += <span class="hljs-number">1</span>
        self.name = name   <span class="hljs-comment">#&#x5B9E;&#x4F8B;&#x53D8;&#x91CF;</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__del__</span><span class="hljs-params">(self)</span>:</span>
        School.population -= <span class="hljs-number">1</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">sayhi</span><span class="hljs-params">(self)</span>:</span>    <span class="hljs-comment">#&#x5B9E;&#x4F8B;&#x65B9;&#x6CD5;</span>
        print(<span class="hljs-string">&apos;hello {}&apos;</span>.format(self.name))

<span class="hljs-meta">    @classmethod</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">howmany</span><span class="hljs-params">(cls)</span>:</span>   <span class="hljs-comment">#&#x7C7B;&#x65B9;&#x6CD5;&#xFF1A;&#x6709;&#x4E00;&#x4E2A;&#x81EA;&#x52A8;&#x8D4B;&#x503C;&#x4E3A;&#x7C7B;&#x540D;&#x7684;&#x5F62;&#x53C2;&#xFF0C;&#x5373;cls&#x4F1A;&#x88AB;&#x81EA;&#x52A8;&#x8D4B;&#x503C;&#x4E3A;&#x7C7B;&#x540D;School</span>
        print(cls.population)
<span class="hljs-meta">    @staticmethod</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">howmany2</span><span class="hljs-params">()</span>:</span>     <span class="hljs-comment">#&#x9759;&#x6001;&#x65B9;&#x6CD5;&#xFF1A;&#x4E0D;&#x9700;&#x8981;&#x4E00;&#x4E2A;&#x81EA;&#x52A8;&#x8D4B;&#x503C;&#x7684;&#x5F62;&#x53C2;</span>
        print(School.population)

s = School(<span class="hljs-string">&apos;erick&apos;</span>)
print(s.name)
s.sayhi()

print(School.population)
School.howmany()
<span class="hljs-keyword">del</span> s
School.howmany2()
</code></pre>
<h2 id="&#x6784;&#x9020;&#x51FD;&#x6570;">&#x6784;&#x9020;&#x51FD;&#x6570;</h2>
<p>&#x5982;&#x679C;&#x6211;&#x4EEC;&#x6CA1;&#x6709;&#x5728;&#x4E00;&#x4E2A;&#x5B50;&#x7C7B;&#x4E2D;&#x5B9A;&#x4E49;<code>__init__</code> &#x65B9;&#x6CD5;&#xFF0C;Python &#x5C06;&#x4F1A;&#x81EA;&#x52A8;&#x8C03;&#x7528;&#x57FA;&#x7C7B;&#x7684;&#x6784;&#x9020;&#x51FD;&#x6570;&#x3002;<br>&#x5982;&#x679C;&#x6211;&#x4EEC;&#x5728;&#x4E00;&#x4E2A;&#x5B50;&#x7C7B;&#x4E2D;&#x5B9A;&#x4E49;&#x4E86;<code>__init__</code> &#x65B9;&#x6CD5;&#xFF0C;Python &#x4E0D;&#x4F1A;&#x81EA;&#x52A8;&#x8C03;&#x7528;&#x57FA;&#x7C7B;&#x7684;&#x6784;&#x9020;&#x51FD;&#x6570;&#xFF0C;&#x4F60;&#x5FC5;&#x987B;&#x81EA;&#x5DF1;&#x663E;&#x5F0F;&#x5730;&#x8C03;&#x7528;&#x5B83;&#x3002;  </p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">A</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self)</span>:</span>
        print(<span class="hljs-string">&apos;A init&apos;</span>)
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">B</span><span class="hljs-params">(A)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self)</span>:</span>
        A.__init__(self)   <span class="hljs-comment">#&#x663E;&#x5F0F;&#x5730;&#x8C03;&#x7528;&#x57FA;&#x7C7B;&#x7684;&#x6784;&#x9020;&#x51FD;&#x6570;</span>
        print(<span class="hljs-string">&apos;B init&apos;</span>)
b = B()
</code></pre>
<blockquote>
<p>&#x6CE8;&#xFF1A;&#x5728;&#x5B50;&#x7C7B;&#x4E2D;&#x8C03;&#x7528;&#x57FA;&#x7C7B;&#x7684;&#x65B9;&#x6CD5;&#x683C;&#x5F0F;&#x4E3A;&#x201C; <code>&#x57FA;&#x7C7B;&#x540D;.&#x65B9;&#x6CD5;&#x540D;</code> &#x201D;&#x3002;</p>
</blockquote>
<h2 id="&#x7C7B;&#x5C5E;&#x6027;&#x548C;&#x5B9E;&#x4F8B;&#x5C5E;&#x6027;">&#x7C7B;&#x5C5E;&#x6027;&#x548C;&#x5B9E;&#x4F8B;&#x5C5E;&#x6027;</h2>
<p>&#x7C7B;&#x5C5E;&#x6027;&#xFF1A;&#x76F4;&#x63A5;&#x5728; class &#x4E2D;&#x5B9A;&#x7684;&#x4E49;&#x5C5E;&#x6027;&#xFF0C;&#x8FD9;&#x4E2A;&#x5C5E;&#x6027;&#x5F52;&#x7C7B;&#x6240;&#x6709;&#xFF0C;&#x5E76;&#x4E14;&#x7C7B;&#x7684;&#x6240;&#x6709;&#x5B9E;&#x4F8B;&#x90FD;&#x53EF;&#x4EE5;&#x8BBF;&#x95EE;&#x5230;&#x3002;<br>&#x5B9E;&#x4F8B;&#x5C5E;&#x6027;&#xFF1A;&#x901A;&#x8FC7;&#x5B9E;&#x4F8B;&#x53D8;&#x91CF;&#xFF0C;&#x6216;&#x8005;&#x901A;&#x8FC7; self &#x53D8;&#x91CF;&#x7ED1;&#x5B9A;&#x7684;&#x5C5E;&#x6027;&#x3002;<br>&#x4F8B;&#x5982;&#xFF1A;  </p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Student</span>:</span>
    school = <span class="hljs-string">&apos;nchu&apos;</span>       <span class="hljs-comment">#&#x7C7B;&#x5C5E;&#x6027;</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, name)</span>:</span>
        self.name = name  <span class="hljs-comment">#&#x901A;&#x8FC7;self&#x53D8;&#x91CF;&#x7ED1;&#x5B9A;&#x7684;&#x5C5E;&#x6027;</span>
s = Student(<span class="hljs-string">&apos;Jack&apos;</span>)

print(Student.school)  <span class="hljs-comment">#&#x8BBF;&#x95EE;&#x7C7B;&#x7684;&#x5C5E;&#x6027;&#xFF0C;&#x8F93;&#x51FA;&#xFF1A;nchu</span>
print(s.school)        <span class="hljs-comment">#&#x8F93;&#x51FA;&#xFF1A;nchu</span>
print(s.name)          <span class="hljs-comment">#&#x8F93;&#x51FA;&#xFF1A;Jack</span>

s.school = <span class="hljs-string">&apos;tinghua&apos;</span>   <span class="hljs-comment">#&#x901A;&#x8FC7;&#x5B9E;&#x4F8B;&#x53D8;&#x91CF;&#x7ED1;&#x5B9A;&#x4E00;&#x4E2A;&#x5B9E;&#x4F8B;&#x5C5E;&#x6027;school&#xFF0C;&#x5728;&#x8FD9;&#x91CC;&#x5B83;&#x548C;&#x7C7B;&#x5C5E;&#x6027;&#x540C;&#x540D;</span>
print(Student.school)  <span class="hljs-comment">#&#x8F93;&#x51FA;&#xFF1A;nchu</span>
print(s.school)        <span class="hljs-comment">#&#x8F93;&#x51FA;&#xFF1A;tinghua</span>

<span class="hljs-keyword">del</span> s.school           <span class="hljs-comment">#&#x5220;&#x9664;&#x5B9E;&#x4F8B;&#x5C5E;&#x6027;</span>
print(s.school)        <span class="hljs-comment">#&#x8F93;&#x51FA;&#xFF1A;nchu</span>
</code></pre>
<p>&#x901A;&#x8FC7;&quot;&#x7C7B;&#x540D;.&#x5C5E;&#x6027;&quot;&#x8BBF;&#x95EE;&#x7C7B;&#x5C5E;&#x6027;&#xFF1B;<br>&#x901A;&#x8FC7;&quot;&#x5B9E;&#x4F8B;&#x53D8;&#x91CF;.&#x5C5E;&#x6027;&quot;&#x8BBF;&#x95EE;&#x5B9E;&#x4F8B;&#x5C5E;&#x6027;&#xFF0C;&#x5982;&#x679C;&#x6CA1;&#x627E;&#x5230;&#xFF0C;&#x5219;&#x7EE7;&#x7EED;&#x8BBF;&#x95EE;&#x7C7B;&#x5C5E;&#x6027;&#xFF1B;&#x5982;&#x679C;&#x5B9E;&#x4F8B;&#x5C5E;&#x6027;&#x548C;&#x7C7B;&#x5C5E;&#x6027;&#x540C;&#x540D;&#xFF0C;&#x5219;&#x5B9E;&#x4F8B;&#x5C5E;&#x6027;&#x5C06;&#x5C4F;&#x853D;&#x6389;&#x76F8;&#x540C;&#x540D;&#x79F0;&#x7684;&#x7C7B;&#x5C5E;&#x6027;&#xFF0C;&#x4F18;&#x5148;&#x4F7F;&#x7528;&#x5B9E;&#x4F8B;&#x5C5E;&#x6027;&#x3002;  </p>
<h2 id="&#x52A8;&#x6001;&#x7ED1;&#x5B9A;">&#x52A8;&#x6001;&#x7ED1;&#x5B9A;</h2>
<p>Python &#x5141;&#x8BB8;&#x52A8;&#x6001;&#x7684;&#x5BF9;&#x5B9E;&#x4F8B;&#x53D8;&#x91CF;&#x7ED1;&#x5B9A;&#x5C5E;&#x6027;&#x548C;&#x65B9;&#x6CD5;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#xFF0C;&#x5BF9;&#x4E8E;&#x4E24;&#x4E2A;&#x5B9E;&#x4F8B;&#x53D8;&#x91CF;&#xFF0C;&#x867D;&#x7136;&#x5B83;&#x4EEC;&#x90FD;&#x662F;&#x540C;&#x4E00;&#x4E2A;&#x7C7B;&#x7684;&#x4E0D;&#x540C;&#x5B9E;&#x4F8B;&#xFF0C;&#x4F46;&#x62E5;&#x6709;&#x7684;&#x53D8;&#x91CF;&#x540D;&#x79F0;&#x90FD;&#x53EF;&#x80FD;&#x4E0D;&#x540C;&#x3002;<br>&#x5C1D;&#x8BD5;&#x7ED9;&#x5B9E;&#x4F8B;&#x7ED1;&#x5B9A;&#x4E00;&#x4E2A;&#x5C5E;&#x6027;&#xFF1A;  </p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Student</span><span class="hljs-params">(object)</span>:</span>
    <span class="hljs-keyword">pass</span>
s = Student()
s.name = <span class="hljs-string">&apos;Michael&apos;</span> <span class="hljs-comment"># &#x52A8;&#x6001;&#x7ED9;&#x5B9E;&#x4F8B;&#x7ED1;&#x5B9A;&#x4E00;&#x4E2A;&#x5C5E;&#x6027;</span>
</code></pre>
<p>&#x8FD8;&#x53EF;&#x4EE5;&#x5C1D;&#x8BD5;&#x7ED9;&#x5B9E;&#x4F8B;&#x7ED1;&#x5B9A;&#x4E00;&#x4E2A;&#x65B9;&#x6CD5;&#xFF1A;</p>
<pre><code class="lang-python">from types import MethodType
def set_age(self, age):
    self.age = age
s.set_age = MethodType(set_age, s) # &#x7ED9;&#x5B9E;&#x4F8B;&#x7ED1;&#x5B9A;&#x4E00;&#x4E2A;&#x65B9;&#x6CD5;
s.set_age(25)                      # &#x8C03;&#x7528;&#x5B9E;&#x4F8B;&#x65B9;&#x6CD5;
&#x4E3A;&#x4E86;&#x7ED9;&#x6240;&#x6709;&#x5B9E;&#x4F8B;&#x90FD;&#x7ED1;&#x5B9A;&#x65B9;&#x6CD5;&#xFF0C;&#x53EF;&#x4EE5;&#x7ED9; class &#x7ED1;&#x5B9A;&#x65B9;&#x6CD5;&#xFF1A;
Student.set_age = MethodType(set_age, Student)
s2 = Student()
s2.set_age(30)
</code></pre>
<h2 id="&#x4F7F;&#x7528;slots">&#x4F7F;&#x7528;<strong>slots</strong></h2>
<p>&#x6709;&#x65F6;&#x5019;&#x9700;&#x8981;&#x9650;&#x5236;&#x5B9E;&#x4F8B;&#x5C5E;&#x6027;&#x7684;&#x52A8;&#x6001;&#x7ED1;&#x5B9A;&#xFF0C;&#x4E3A;&#x4E86;&#x8FBE;&#x5230;&#x9650;&#x5236;&#x7684;&#x76EE;&#x7684;&#xFF0C; Python &#x5141;&#x8BB8;&#x5728;&#x5B9A;&#x4E49; class &#x7684;&#x65F6;&#x5019;&#xFF0C;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x7279;&#x6B8A;&#x7684;<code>__slots__</code>&#x53D8;&#x91CF;&#xFF0C;&#x6765;&#x9650;&#x5236;&#x8BE5; class &#x5B9E;&#x4F8B;&#x80FD;&#x6DFB;&#x52A0;&#x7684;&#x5C5E;&#x6027;&#xFF1A;  </p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Student</span><span class="hljs-params">(object)</span>:</span>
    __slots__ = (<span class="hljs-string">&apos;name&apos;</span>, <span class="hljs-string">&apos;age&apos;</span>) 
    <span class="hljs-comment"># &#x7528; tuple &#x5B9A;&#x4E49;&#x6240;&#x6709;&#x5141;&#x8BB8;&#x7ED1;&#x5B9A;&#x7684;&#x5C5E;&#x6027;&#x540D;&#x79F0;&#xFF0C;&#x8FD9;&#x91CC;&#x8868;&#x793A;&#x53EA;&#x5141;&#x8BB8;&#x6DFB;&#x52A0;name&#x548C;age&#x4E24;&#x4E2A;&#x5C5E;&#x6027;</span>
</code></pre>
<p>&#x9700;&#x8981;&#x6CE8;&#x610F;&#xFF0C; <code>__slots__</code>&#x5B9A;&#x4E49;&#x7684;&#x5C5E;&#x6027;&#x4EC5;&#x5BF9;&#x5F53;&#x524D;&#x7C7B;&#x5B9E;&#x4F8B;&#x8D77;&#x4F5C;&#x7528;&#xFF0C;&#x5BF9;&#x7EE7;&#x627F;&#x7684;&#x5B50;&#x7C7B;&#x662F;&#x4E0D;&#x8D77;&#x4F5C;&#x7528;&#x7684;&#xFF1B;&#x9664;&#x975E;&#x5728;&#x5B50;&#x7C7B;&#x4E2D;&#x4E5F;&#x5B9A;&#x4E49;<code>__slots__</code>&#xFF0C;&#x8FD9;&#x6837;&#xFF0C;&#x5B50;&#x7C7B;&#x5B9E;&#x4F8B;&#x5141;&#x8BB8;&#x5B9A;&#x4E49;&#x7684;&#x5C5E;&#x6027;&#x5C31;&#x662F;&#x81EA;&#x8EAB;&#x7684;<code>__slots__</code>&#x52A0;&#x4E0A;&#x7236;&#x7C7B;&#x7684;<code>__slots__</code>&#x3002;</p>
<h2 id="&#x4F7F;&#x7528;property">&#x4F7F;&#x7528;@property</h2>
<p>Python &#x5185;&#x7F6E;&#x7684;@property &#x88C5;&#x9970;&#x5668;&#x662F;&#x8D1F;&#x8D23;&#x628A;&#x4E00;&#x4E2A;&#x65B9;&#x6CD5;&#x53D8;&#x6210;&#x5C5E;&#x6027;&#x8C03;&#x7528;&#x7684;&#x3002;@property &#x5E7F;&#x6CDB;&#x5E94;&#x7528;&#x5728;&#x7C7B;&#x7684;&#x5B9A;&#x4E49;&#x4E2D;&#xFF0C;&#x53EF;&#x4EE5;&#x8BA9;&#x8C03;&#x7528;&#x8005;&#x5199;&#x51FA;&#x7B80;&#x77ED;&#x7684;&#x4EE3;&#x7801;&#xFF0C;&#x540C;&#x65F6;&#x4FDD;&#x8BC1;&#x5BF9;&#x53C2;&#x6570;&#x8FDB;&#x884C;&#x5FC5;&#x8981;&#x7684;&#x68C0;&#x67E5;&#xFF0C;&#x8FD9;&#x6837;&#xFF0C;&#x7A0B;&#x5E8F;&#x8FD0;&#x884C;&#x65F6;&#x5C31;&#x51CF;&#x5C11;&#x4E86;&#x51FA;&#x9519;&#x7684;&#x53EF;&#x80FD;&#x6027;&#x3002;  </p>
<p>&#x628A;&#x4E00;&#x4E2A; getter &#x65B9;&#x6CD5;&#x53D8;&#x6210;&#x5C5E;&#x6027;&#xFF0C;&#x53EA;&#x9700;&#x8981;&#x52A0;&#x4E0A;<code>@property</code> &#x5C31;&#x53EF;&#x4EE5;&#x4E86;&#xFF1B;<br>&#x5728;&#x8FD9;&#x4E2A;&#x57FA;&#x7840;&#x4E0A;&#x8FD8;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x53E6;&#x4E00;&#x4E2A;&#x88C5;&#x9970;&#x5668;<code>@score.setter</code>&#x628A;&#x4E00;&#x4E2A; setter &#x65B9;&#x6CD5;&#x53D8;&#x6210;&#x5C5E;&#x6027;&#x3002;<br>&#x53EA;&#x5B9A;&#x4E49; getter &#x65B9;&#x6CD5;&#xFF0C;&#x4E0D;&#x5B9A;&#x4E49; setter &#x65B9;&#x6CD5;&#x5C31;&#x662F;&#x4E00;&#x4E2A;&#x53EA;&#x8BFB;&#x5C5E;&#x6027;&#x3002;  </p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Stu</span>:</span>
<span class="hljs-meta">    @property #getter&#x65B9;&#x6CD5;</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">val</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-keyword">return</span> self.__val
<span class="hljs-meta">    @val.setter  #setter&#x65B9;&#x6CD5;</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">val</span><span class="hljs-params">(self, value)</span>:</span>
        <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> isinstance(value, int):
            <span class="hljs-keyword">raise</span> ValueError(<span class="hljs-string">&apos;val must be an integer.&apos;</span>)
        <span class="hljs-keyword">if</span> value &lt; <span class="hljs-number">0</span> <span class="hljs-keyword">or</span> value &gt; <span class="hljs-number">100</span>:
            <span class="hljs-keyword">raise</span> ValueError(<span class="hljs-string">&apos;val is 0~100&apos;</span>)
        self.__val = value
<span class="hljs-meta">    @property   #getter&#x65B9;&#x6CD5;</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">age</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-keyword">return</span> <span class="hljs-number">100</span>
s = Stu()
s.val = <span class="hljs-number">100</span>   <span class="hljs-comment">#&#x5B9E;&#x9645;&#x8F6C;&#x5316;&#x4E3A;s.set_val(100)</span>
print(s.val)  <span class="hljs-comment">#&#x5B9E;&#x9645;&#x8F6C;&#x5316;&#x4E3A;s.get_val()</span>
print(s.age)  <span class="hljs-comment">#&#x5B9E;&#x9645;&#x8F6C;&#x5316;&#x4E3A;s.get_age()</span>
</code></pre>
<h2 id="&#x591A;&#x91CD;&#x7EE7;&#x627F;">&#x591A;&#x91CD;&#x7EE7;&#x627F;</h2>
<p>&#x901A;&#x8FC7;&#x591A;&#x91CD;&#x7EE7;&#x627F;&#xFF0C;&#x4E00;&#x4E2A;&#x5B50;&#x7C7B;&#x5C31;&#x53EF;&#x4EE5;&#x540C;&#x65F6;&#x83B7;&#x5F97;&#x591A;&#x4E2A;&#x7236;&#x7C7B;&#x7684;&#x6240;&#x6709;&#x529F;&#x80FD;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Child</span><span class="hljs-params">(Father1, Father2, Father3)</span>:</span>
    <span class="hljs-keyword">pass</span>
</code></pre>
<p>&#x5728;&#x8BBE;&#x8BA1;&#x7C7B;&#x7684;&#x7EE7;&#x627F;&#x5173;&#x7CFB;&#x65F6;&#xFF0C;&#x901A;&#x5E38;&#xFF0C;&#x4E3B;&#x7EBF;&#x90FD;&#x662F;&#x5355;&#x4E00;&#x7EE7;&#x627F;&#x4E0B;&#x6765;&#x7684;.&#x5982;&#x679C;&#x9700;&#x8981;&#x201C;&#x6DF7;&#x5165;&#x201D;&#x989D;&#x5916;&#x7684;&#x529F;&#x80FD;&#xFF0C;&#x901A;&#x8FC7;&#x591A;&#x91CD;&#x7EE7;&#x627F;&#x5C31;&#x53EF;&#x4EE5;&#x5B9E;&#x73B0;&#x3002;&#x8FD9;&#x79CD;&#x8BBE;&#x8BA1;&#x901A;&#x5E38;&#x79F0;&#x4E4B;&#x4E3A; MixIn&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="函数式编程.html" class="navigation navigation-prev " aria-label="Previous page: 函数式编程">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="类的特殊方法.html" class="navigation navigation-next " aria-label="Next page: 类的特殊方法">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"面向对象编程","level":"1.8","depth":1,"next":{"title":"类的特殊方法","level":"1.9","depth":1,"path":"类的特殊方法.md","ref":"类的特殊方法.md","articles":[]},"previous":{"title":"函数式编程","level":"1.7","depth":1,"path":"函数式编程.md","ref":"函数式编程.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"面向对象编程.md","mtime":"2017-08-23T14:55:31.458Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2017-08-27T04:11:18.522Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

